#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets
from ui_setting import Ui_DlgSettingClass
from PyQt5.QtWidgets import QFileDialog
import config_json
import os
import logging

class DlgSetting(QtWidgets.QDialog):
    def __init__(self, parent=None):
        logging.info("init")
        super(DlgSetting, self).__init__(parent)
        self.ui = Ui_DlgSettingClass()
        self.ui.setupUi(self)
        # self.ui.frame.setVisible(False)
        database_path = config_json.setting["database"]
        self.ui.lineEdit.setText(database_path)
        output_folder = config_json.setting["output_folder"]
        self.ui.lineEdit_2.setText(output_folder)
        yqbh = str(config_json.setting["yqbh"])
        self.ui.lineEdit_yqbh.setText(yqbh)
        self.ui.lineEdit_ejff.setText(config_json.setting["ejff_file"])
        # self.ui.label_sblx.setText(config_json.setting["sblx"])
        at = 0
        ii = 0
        sblx = config_json.setting["sblx"]
        for i in config_json.setting["sblx_list"]:
            if i == sblx:
                at = ii
            self.ui.cmb_sblx.addItem(i)
            ii += 1
        self.ui.cmb_sblx.setCurrentIndex(at)
        self.ui.cmb_sblx.setEditable(True)

        self.ui.cmb_sblx.currentIndexChanged[int].connect(self.sblx_change)
        self.ui.btnSelect.clicked.connect(self.select)
        self.ui.btnOutput.clicked.connect(self.select2)
        if config_json.setting["upgrade"]==1:
            pass
        else:
            self.ui.checkBox_upgrade.setVisible(False)
        self.ui.checkBox.setVisible(False)
        # if config_json.setting["lr"]==1:
        #     self.ui.checkBox.setChecked(True)
        # else:
        #     self.ui.checkBox.setChecked(False)
        if config_json.setting["upgrade"]==1:    
            if config_json.setting["autoUpgrade"]==1:
                self.ui.checkBox_upgrade.setChecked(True)
            else:
                self.ui.checkBox_upgrade.setChecked(False)
        else:
            self.ui.checkBox_upgrade.setVisible(False)
        self.ui.pushButton_ok.clicked.connect(self.ok)
        self.ui.pushButton_cancel.clicked.connect(self.cancel)
        self.ui.pushButton_ejff.clicked.connect(self.select_ejff)
    def ok(self):
        self.accept()
    def cancel(self):
        self.reject()
    def sblx_change(self, at):
        # self.ui.label_sblx.setText(self.ui.cmb_sblx.itemText(at));
        pass

    def select(self):
        FileDialog = QFileDialog(self)
        FileDirectory = FileDialog.getOpenFileName(self, '选择数据库文件')  # 选择目录，返回选中的路径
        logging.info(FileDirectory)
        self.ui.lineEdit.setText(FileDirectory[0])
    def select_ejff(self):
        FileDialog = QFileDialog(self)
        FileDirectory = FileDialog.getOpenFileName(self, '选择二级方法文件')  # 选择目录，返回选中的路径
        logging.info(FileDirectory)
        self.ui.lineEdit_ejff.setText(FileDirectory[0])
    def select2(self):
        FileDialog = QFileDialog(self)
        FileDirectory = FileDialog.getExistingDirectory(self, 'select output folder')  # 选择目录，返回选中的路径
        # logging.info(FileDirectory, type(FileDirectory), dir(FileDirectory))
        if FileDirectory != "":
            self.ui.lineEdit_2.setText(FileDirectory)
        else:
            logging.info("not select")

    # @QtCore.pyqtSlot()
    def accept(self):
        logging.info("accept")
        config_json.setting['database']= self.ui.lineEdit.text()
        config_json.setting['output_folder']= self.ui.lineEdit_2.text()
        config_json.setting['yqbh']= self.ui.lineEdit_yqbh.text()
        # config_json.setting['sblx']=self.ui.cmb_sblx.currentText()
        config_json.setting["sblx"]= self.ui.cmb_sblx.currentText()
        config_json.setting["ejff_file"]= self.ui.lineEdit_ejff.text()
        if self.ui.checkBox.isChecked():
            config_json.setting["lr"]=1
        else:
            config_json.setting["lr"]=0
        if config_json.setting["upgrade"]==1:
            if self.ui.checkBox_upgrade.isChecked():
                config_json.setting["autoUpgrade"]=1
            else:
                config_json.setting["autoUpgrade"]=0
        if self.ui.cmb_sblx.currentText() in ["uni_cs_msg", "uni_on_msg"]:
            self.output_myconfig(self.ui.lineEdit.text())
            pass  # save myconfig
        self.done(1)
        pass

    def output_myconfig(self, database_path):
        ps = database_path.split("/")
        r = ""
        for i in ps[:-3]:
            r = r + i + "\\"
        f = open("myconfig.py", "w", encoding="utf-8")
        f.write("""uni_bin_path=r'%s'
uni_db_path=r'%s'""" % (os.path.join(r, "Bin"), database_path.replace("/", "\\")))
        f.close()

    def reject(self):
        logging.info("reject")
        self.done(0)
        pass


def main():
    import sys
    app = QtWidgets.QApplication(sys.argv)
    calculator = DlgSetting()
    calculator.exec()


if __name__ == '__main__':
    main()
